<?php

class Resident extends Member implements PermissionProvider
{
    private static $singular_name = "Resident Profile";

    private static $plural_name = "Resident Profiles";

    private static $db = array(
        "UnitNo" => "Varchar(100)",
        "AreaName" => "Varchar(100)",
        "ContactNumber" => "Varchar(50)",
    );

    private static $has_one = array(
        "SnPUpload" => "File"
    );

    private static $searchable_fields = array(
        "FirstName",
        "Username",
        "Email",
        "ContactNumber",
        "AccountStatus"
    );

    private static $summary_fields = array(
        "FirstName",
        "Username",
        "Email",
        "ContactNumber",
        "FormattedAccountStatus",
        "Created.Nice24"
    );

    private static $default_sort = "ID DESC";

    private static $field_labels = array(
        "FormattedAccountStatus" => "Account Status",
        "Created.Nice24" => "Registered At"
    );

    public function canView($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "VIEW_Resident"
        ), "any", $member);
    }

    public function canEdit($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "EDIT_Resident"
        ), "any", $member);
    }

    public function canDelete($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "DELETE_Resident"
        ), "any", $member);
    }

    public function canCreate($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "CREATE_Resident"
        ), "any", $member);
    }

    public function FormattedAccountStatus()
    {
        return MemberExtension::GetFormattedAccountStatus($this->AccountStatus);
    }

    public function getDefaultSearchContext()
    {
        $context = parent::GetDefaultSearchContext();

        $context->removeFieldByName("AccountStatus");
        $context->addField(MemberExtension::GetAccountStatusField(true));

        return $context;
    }

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $fields->insertAfter(new TextField("Username", "Username"), "Email");

        $fields->insertAfter(new TextField("Created", "Registered At"), "LastVisited");
        $fields->makeFieldReadonly("Created");

        $fields->replaceField("Gender", MemberExtension::GetGenderField());

        $fields->replaceField("AreaName", new DropdownField
        (
            "AreaName",
            _t("General.AREA_NAME", "Area Name"),
            array(
                "A1" => "Area 1",
                "A2" => "Area 2",
                "A3" => "Area 3"
            )
        ));

        $fields->replaceField("AccountStatus", MemberExtension::GetAccountStatusField());

        if ($this->isInDB()) {
            if ($this->SnPUpload()->ID) {
                $snpFilePath = $this->SnPUpload()->getAbsoluteURL();

                if (is_array(getimagesize($snpFilePath))) {
                    // Is image file.
                    $snpFileHtml = new LiteralField("SnPUploadImage", '<div class="field text"><label class="left">S&P Document Preview</label><br/><img alt="" src="' . $snpFilePath . '" style="border: 1px #b3b3b3 solid;" /></div>');
                } else {
                    // Not image file.
                    $snpFileHtml = new LiteralField("SnPUploadImage", '<div class="field text"><label class="left">S&P Document Preview</label><br/>Preview not available. <a href="' . $snpFilePath . '" target="_blank">Download file</a></div>');
                }
            } else {
                // Not file is uploaded.
                $snpFileHtml = new LiteralField("SnPUploadImage", '<div class="field text"><label class="left">S&P Document Preview</label><br/>No file is uploaded. Preview not available.</div>');
            }

            $fields->addFieldToTab("Root.S&P Document", $snpFileHtml);
        }

        if ($this->canCreate() || $this->canEdit()) {
            $snpUploadField = new UploadField("SnPUpload", "Options");
            $snpUploadField->setAllowedExtensions(array("jpg", "png", "pdf", "docx", "zip", "rar", "tar", "gz", "tgz", "doc"));
            $snpUploadField->setFolderName("Uploads/resident-snp");

            $fields->addFieldToTab("Root.S&P Document", $snpUploadField);
        } else {
            $fields->removeByName("SnPUpload");
        }

        $fields->removeByName(array(
            "Locale",
            "DirectGroups",
            "Permissions",
            "DateFormat",
            "TimeFormat"
        ));

        return $fields;
    }

    public function onAfterWrite()
    {
        $mem = Member::get()->byID($this->ID);

        if (!$mem->inGroup("resident")) {
            $mem->addToGroupByCode("resident", "Resident");
        }

        parent::onAfterWrite();
    }

    public function providePermissions()
    {
        $category = "Manage resident permissions";

        return array(
            "VIEW_Resident" => array(
                "name" => "View all records",
                "category" => $category
            ),
            "CREATE_Resident" => array(
                "name" => "Create new record",
                "category" => $category
            ),
            "EDIT_Resident" => array(
                "name" => "Edit record",
                "category" => $category
            ),
            "DELETE_Resident" => array(
                "name" => "Delete record",
                "category" => $category
            )
        );
    }
}